package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:BrandController
 * Package:com.spzx.product.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/20 - 9:23
 */
@Tag(name = "品牌接口管理")
@RequestMapping("/brand")
@RestController
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌列表
     *
     * @param brand
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getPageList(Brand brand) {
        startPage();
        List<Brand> brandList = brandService.getPageList(brand);
        return getDataTable(brandList);
    }

    /**
     * 获取品牌详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getBrandById(@PathVariable Long id) {
        Brand brand = brandService.getById(id);
        return AjaxResult.success(brand);
    }

    /**
     * 获取全部品牌
     *
     * @return
     */
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll() {
        List<Brand> brandList = brandService.list();
        return AjaxResult.success(brandList);
    }

    /**
     * 新增品牌
     *
     * @param brand
     * @return
     */
    @PostMapping("")
    public AjaxResult addBrand(@RequestBody @Validated Brand brand) {
        brand.setDelFlag("0");
        boolean b = brandService.save(brand);
        return toAjax(b);
    }

    /**
     * 修改品牌
     *
     * @param brand
     * @return
     */
    @PutMapping("")
    public AjaxResult updateBrand(@RequestBody @Validated Brand brand) {
        brand.setUpdateBy(SecurityUtils.getUsername());
        boolean b = brandService.updateBrand(brand);
        return toAjax(b);
    }

    /**
     * 删除品牌
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(brandService.deleteBrandByIds(ids));
    }
}
